﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SCResources.BusinissLayer;
using SCResources.General;
using Serie_Counter.Code;
using Serie_Counter.Presentatie;
using Serie_Counter.Presentatie.GuiHelpers;

namespace Serie_Counter.Forms

{
    /// <summary>
    /// Main form, contains the active series & top menu
    /// </summary>
    public partial class MainForm : Form
    {

        private ScResources manager;
        private UserControlHelper uCHelper;
        private ShowFormDel showFormDel;
        //private updateForm updateForm;

        public UserControlHelper UCHelper
        {
            get
            {
                return uCHelper;
            }
        }

        public MainForm(ScResources manager)
        {
            InitializeComponent();
            this.manager = manager;

            CreateSerieThumbNails();
        }

        ~MainForm()
        {
        }

        #region Toolstrip menu items


        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "xml Files (*.xml)|*.xml";
            saveFile.ShowDialog();
            if (saveFile.FileName != "")
            {
                // manager.Export(saveFile.FileName);
            }
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "xml Files (*.xml)|*.xml";
            openFile.ShowDialog();
            if (openFile.FileName != "")
            {
                // manager.import(openFile.FileName);
            }

            uCHelper.placeUserControls();
        }        

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // manager.saveCurrentContent();
        }

        private void archiveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showFormDel(CallForm.Archived);
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //SettingsForm settingsForm = new SettingsForm(manager.Settings());
            //if (settingsForm.ShowDialog() == DialogResult.OK)
            //{
            //    manager.setSettings(settingsForm.Settings);
            //}
            showFormDel(CallForm.Settings);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm about = new AboutForm();
            about.ShowDialog();
        }

        #endregion

        #region Serie info buttons

        private void buttonAddSerie_Click(object sender, EventArgs e)
        {
            SerieForm serieForm = new SerieForm(manager); 
            if(serieForm.ShowDialog() == DialogResult.OK)
            {
                //todo: refresh bar with series.
                AddThumbNail(serieForm.Serie);
                
                if(serieForm.Serie.AutoAirInfo) manager.GetLastAirdateAsync(serieForm.Serie);
            }
        }

        #endregion


        #region Thumbnail view

        private void CreateSerieThumbNails()
        {
            List<SerieWrapper> allSeries = manager.GetAll();
            ImageList imageList = new ImageList();

            for(int i = 0; i < allSeries.Count ; i++ )
            {
                Image image = Image.FromFile(allSeries[i].CoverPath);
                imageList.Images.Add(image);
            }

            imageList.ImageSize = new Size(100,140);
            listViewThumbs.LargeImageList = imageList;
            
            for (int i = 0; i < imageList.Images.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = i;
                item.Name = allSeries[i].DbId.ToString();
                item.Text = allSeries[i].Title;

                listViewThumbs.Items.Add(item);
            }
        }

        private void AddThumbNail(SerieWrapper serie)
        {
            listViewThumbs.LargeImageList.Images.Add(serie.Cover);

            ListViewItem item = new ListViewItem();
            item.ImageIndex = listViewThumbs.LargeImageList.Images.Count - 1;
            item.Name = serie.DbId.ToString();
            item.Text = serie.Title;

            listViewThumbs.Items.Add(item);
        }

        private void RemoveThumbNail(int Id)
        {
            throw new NotImplementedException();//todo : implement
        }

        //todo: create add and remove method for ListView so no total refresh

        private void FillSerieInfo(int id)
        {
            //air info
            SerieWrapper serie = manager.Get(id);
            labelTitle.Text = serie.Title;

            if (serie.AirInfo != null)
            {
                labelAirDate.Text = serie.AirInfo.NextAirDate.ToShortDateString(); //todo: add If for null
                labelNextEpisode.Text = CreatEpisodeString(serie.AirInfo.Season, serie.AirInfo.Episode);

                if (serie.AirInfo.Frequency == -1)
                {
                    labelFrequency.Visible = false;
                    labelFrequencyStatic.Visible = false;
                }
                else
                {
                    labelFrequency.Visible = true;
                    labelFrequencyStatic.Visible = true;
                    labelFrequency.Text = serie.AirInfo.Frequency.ToString();
                }
            }
            else
            {
                labelAirDate.Text = "No info available";
                labelNextEpisode.Text = "No info available";
                labelFrequency.Text = "No info available";
            }

            //user info
            labelSeason.Text = serie.UserInfo.Season.ToString();
            labelEpisode.Text = serie.UserInfo.Episode.ToString();
        }

        private string CreatEpisodeString(int season, int episode)
        {
            StringBuilder s = new StringBuilder();
            s.Append("S");

            if(season < 10)
            {
                s.Append("0" + season.ToString());
            }
            else
            {
                s.Append(season.ToString());
            }

            s.Append("E");

            if(episode < 10)
            {
                s.Append("0" + episode.ToString());
            }
            else
            {
                s.Append(episode.ToString());
            }

            return s.ToString();

        }

        private void listViewThumbs_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListView listView = (ListView)sender;

            if (listView.SelectedItems.Count !=0)
            {
                FillSerieInfo(int.Parse(listView.SelectedItems[0].Name));
            }
        }
        
        #endregion

       

        


        //Modules:
        //List module:
        // -show all series
        // -sortable
        // -change color if new episode is available
        // -Add a serie

        //overview module:
        // -show information about the serie
        // -remove/edit series
        // -increment users S/E number
        // -archive

        //Local files module
        // -display serie file that are found
        // -play with default media player of user
        // -change color when played
        // -auto incr user S/E number when that episode is viewed
    }
}
